<?php

namespace App\System\Base\Repositories;

use App\Module\Api\Controllers\Request\PraiseSwitchRequest;
use App\Module\Api\Controllers\Request\QueryRequest;
use App\Module\Api\Controllers\Request\TableRequest;
use App\System\Base\Helpers\Traits\SinglePattern;
use App\System\Base\Model\BaseNotificationRecord;
use App\System\Order\Repositories\OrderInfoRepository;

class BaseNotificationRecordRepository extends Repository
{
    use SinglePattern;

    /**
     * 列表
     * @param $request object
     * @return mixed
     */
    public function getApiList(QueryRequest $queryRequest,TableRequest $tableRequest,$master_id){
        $type = $queryRequest->action ? $queryRequest->action : '';
        $data = BaseNotificationRecord::when($type,function($query) use($type){
            return $query->where('type',$type);
            })
            ->where("user_id",$master_id)
            ->orderBy($tableRequest->orderBy,$tableRequest->orderType)
            ->paginate($tableRequest->limit,['*'],'page',$tableRequest->page);

        $order = OrderInfoRepository::getInstance();
        foreach($data as $v)
        {
            $item = [];
            $item['id'] = $v->id;
            $item['title'] = $v->title;
            $item['content'] = $v->content;
            $item['time'] = $v->created_at->toDateTimeString();
            if($v->type == 2)
            {
                $order_info = $order->getOne($v->from_id,false);
                if($order_info)
                {
                    $goods = $order_info['merchant']['orderGoodses'];
                    $goodses[] = ['id'=>$goods['id'],'goods_name'=>$goods['name'],'goodsImage'=>$goods['imagePath']];
                    $order = ['id'=>$order_info['id'],'orderNo'=>$order_info['orderSn'],'goodses'=>$goodses];
                    $item['order'] = $order;
                }
            }
            $return['messages'][] = $item;
        }
        $return['total'] =$data->total();
        return $return;
    }

    /**
     * 查询未读消息数
     * @param $user_id
     */
    public function getUnreadNumber($user_id)
    {
        $sysMessageNumber_number = BaseNotificationRecord::where('type',1)
            ->where("user_id",$user_id)->paginate(1,['*'],'page',1);
        $orderMessageNumber = BaseNotificationRecord::where('type',2)
            ->where("user_id",$user_id)->paginate(1,['*'],'page',1);

        return ['sysMessageNumber'=>$sysMessageNumber_number->total(),'orderMessageNumber'=>$orderMessageNumber->total()];
    }
    /**
     * 状态更新
     * @param PraiseSwitchRequest $praiseSwitchRequest
     * @param $master_id
     * @return mixed
     */
    public function updateStatus(PraiseSwitchRequest $praiseSwitchRequest,$master_id)
    {
        $id = $praiseSwitchRequest->id ? $praiseSwitchRequest->id : null;
        $res = BaseNotificationRecord::where('user_id',$master_id)
            ->where("type",$praiseSwitchRequest->action)
            ->when($id,function($query) use($id){
              return  $query->where('id',$id);
            })
            ->update(['status'=>2]);
        return $res;
    }

    /**
     * 生成记录
     */
    public function addData($set)
    {
       return BaseNotificationRecord::create($set);
    }
}